package io.github.loan.common.util;

import java.io.IOException;
import java.util.Iterator;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 爬虫
 * 解析HTML
 * 
 * jsoup
 * @author Andone
 *
 */
public class HtmlParseUtil {
	/**
	 * 解析百度新闻
	 * @param url
	 * @return
	 * 
	 *{"newsList":[{},{},{}]}
	 */
	public JSONObject parseBaiduNews(String url){
		JSONObject resultJSON=new JSONObject();
		
		try {
			/*请求url*/
			Connection connection =Jsoup.connect(url);
			/*获取Document对象*/
			Document document=connection.get();
			/*获取指定的元素*/
			Element bdEle=document.getElementsByClass("mod-instantNews").get(0);
			Elements itemEles=bdEle.getElementsByClass("item"); 
			
			/* 将结果存储到JSON中 */
			JSONArray newsArr=new  JSONArray();
			/* 整体DIV的内容(news) */
			for (Iterator iterator = itemEles.iterator(); iterator.hasNext();) {
				Element ele = (Element) iterator.next();
				JSONObject newsJSON=new JSONObject();
				/* text():拿到开始标签和结束标签中的内容:去掉标签 */
				String title=ele.getElementsByAttributeValue("class","title").text();
				if(!"".equalsIgnoreCase(title)){
					newsJSON.put("title", title);
				}
				Elements contentEles = ele.getElementsByClass("detail") ;
				if(contentEles.size() > 0 ){
					String content = contentEles.get(0).text();
					newsJSON.put("content", content);
				}
				/* 放到容器中 */
				newsArr.add(newsJSON);
			}
			resultJSON.put("newsList", newsArr);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return resultJSON ;
	}
	public static void main(String[] args) {
		HtmlParseUtil htmlParseUtil=new HtmlParseUtil();
		String url = "http://internet.baidu.com/" ;
		JSONObject resultJSON = htmlParseUtil.parseBaiduNews(url);
		System.out.println(resultJSON);
	}
}
